package rexsee.activity;

import java.io.File;
import java.io.FileOutputStream;

import rexsee.core.style.RexseeDrawable;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class WelcomeDialog extends Dialog {

	private static WelcomeDialog theDialog = null;

	private final ImageView view;

	public WelcomeDialog(final Context context, Drawable drawable, final String updatePath) {

		super(context, android.R.style.Theme_Panel);

		view = new ImageView(context);
		view.setImageDrawable(drawable);

		Window window = this.getWindow();
		window.requestFeature(Window.FEATURE_NO_TITLE);
		this.setCancelable(true);
		this.setOnCancelListener(new DialogInterface.OnCancelListener() {
			@Override
			public void onCancel(DialogInterface dialog) {
				((Activity) context).finish();
			}
		});
		this.setCanceledOnTouchOutside(false);
		this.setContentView(view);

		show();

		WindowManager.LayoutParams attr = window.getAttributes();
		attr.type = WindowManager.LayoutParams.TYPE_APPLICATION;
		attr.windowAnimations = android.R.style.Animation_Dialog;
		attr.width = ViewGroup.LayoutParams.FILL_PARENT;
		attr.height = ViewGroup.LayoutParams.FILL_PARENT;
		window.setAttributes(attr);

		theDialog = this;

		if (updatePath != null) {
			final String picPath = getPath(context);
			new Thread() {
				@Override
				public void run() {
					try {
						final BitmapDrawable newDrawable = (BitmapDrawable) RexseeDrawable.getDrawable(context, null, updatePath, null);
						if (newDrawable == null) return;
						Bitmap bitmap = newDrawable.getBitmap();
						File file = new File(Uri.parse(picPath).getPath());
						FileOutputStream fileOutput = new FileOutputStream(file);
						boolean rtn = bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fileOutput);
						fileOutput.flush();
						fileOutput.close();
						if (rtn) {
							((Activity) context).runOnUiThread(new Runnable() {
								@Override
								public void run() {
									view.setImageDrawable(newDrawable);
								}
							});
						}
					} catch (Exception e) {
					}
				}
			}.start();
		}

	}

	public static void showWelcome(Context context) {

		Resources r = context.getResources();

		int id = r.getIdentifier("app_welcome", "string", context.getPackageName());
		String updatePath = (id == 0) ? null : r.getString(id);

		Drawable drawable = RexseeDrawable.getDrawable(context, null, getPath(context), null);
		if (drawable != null) {
			new WelcomeDialog(context, drawable, updatePath);
		} else {
			int aid = r.getIdentifier("welcome", "drawable", context.getPackageName());
			if (aid != 0) {
				new WelcomeDialog(context, r.getDrawable(aid), updatePath);
			} else {
			}
		}

	}

	private static String getPath(Context context) {
		return "file://" + context.getCacheDir().getParent() + "/welcome.jpg";
	}

	public static void hideWelcome() {
		if (theDialog != null) {
			theDialog.dismiss();
			theDialog = null;
		}
	}

}
